using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKDVERB
    {
        public DataRow Convert2KDVERB(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKDVERB(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KDVBMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KDVBKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["KDVBVB"] = " ".PadRight(3 ); 
         dwReturn["KDVBVG"] = " ".PadRight(3 ); 
         dwReturn["KDVBVERBNR"] = " ".PadRight(8 ); 
         dwReturn["KDVBMITGLNR"] = " ".PadRight(16 ); 
         dwReturn["KDVBKONTONR"] = " ".PadRight(10 ); 
         dwReturn["KDVBMITGLNAM"] = " ".PadRight(40 ); 
         dwReturn["KDVBZENTRR"] = " ".PadRight(1 ); 
         dwReturn["KDVBSTAKZ"] = " ".PadRight(1 ); 
         dwReturn["KDVBSTADAT"] = " ".PadRight(0 ); 
         dwReturn["KDVBUVERBKZ"] = " ".PadRight(1 ); 
         dwReturn["KDVBKOND"] = " ".PadRight(1 ); 
         dwReturn["KDVBDATVON"] = " ".PadRight(0 ); 
         dwReturn["KDVBDATBIS"] = " ".PadRight(0 ); 
         dwReturn["KDVBMAILGKZ"] = " ".PadRight(1 ); 
         dwReturn["KDVBFILLER"] = " ".PadRight(67 ); 
         dwReturn["KDVBAENDZL"] = " ".PadRight(0 ); 
         dwReturn["KDVBAENDBEN"] = " ".PadRight(8 ); 
         dwReturn["KDVBAENDDAT"] = " ".PadRight(0 ); 
         dwReturn["KDVBNEUBEN"] = " ".PadRight(8 ); 
         dwReturn["KDVBNEUDAT"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKDVERB(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKDVERB(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKDVERB(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKDVERB(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKDVERB(dwLocal, ITXDBVERSION);
            }
            UpdateKDVERB(dwLocal);
        }
        public void InsertKDVERB(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KDVERB(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKDVERB(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KDVERB WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KDVERB  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KDVERB";
            Return = Result;
            return Return;
        }
        public void DeleteKDVERB(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KDVERB   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKDVERB(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KDVERB SET "
                    +"KDVBMANDANT= :KDVBMANDANT,"
                    +"KDVBKUNDNR= :KDVBKUNDNR,"
                    +"KDVBVB= :KDVBVB,"
                    +"KDVBVG= :KDVBVG,"
                    +"KDVBVERBNR= :KDVBVERBNR,"
                    +"KDVBMITGLNR= :KDVBMITGLNR,"
                    +"KDVBKONTONR= :KDVBKONTONR,"
                    +"KDVBMITGLNAM= :KDVBMITGLNAM,"
                    +"KDVBZENTRR= :KDVBZENTRR,"
                    +"KDVBSTAKZ= :KDVBSTAKZ,"
                    +"KDVBSTADAT= :KDVBSTADAT,"
                    +"KDVBUVERBKZ= :KDVBUVERBKZ,"
                    +"KDVBKOND= :KDVBKOND,"
                    +"KDVBDATVON= :KDVBDATVON,"
                    +"KDVBDATBIS= :KDVBDATBIS,"
                    +"KDVBMAILGKZ= :KDVBMAILGKZ,"
                    +"KDVBFILLER= :KDVBFILLER,"
                    +"KDVBAENDZL= :KDVBAENDZL,"
                    +"KDVBAENDBEN= :KDVBAENDBEN,"
                    +"KDVBAENDDAT= :KDVBAENDDAT,"
                    +"KDVBNEUBEN= :KDVBNEUBEN,"
                    +"KDVBNEUDAT= :KDVBNEUDAT,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KDVBMANDANT", OracleType.NVarChar).Value = dwData["KDVBMANDANT"].ToString(); 
          or1.Parameters.Add("KDVBKUNDNR", OracleType.NVarChar).Value = dwData["KDVBKUNDNR"].ToString(); 
          or1.Parameters.Add("KDVBVB", OracleType.NVarChar).Value = dwData["KDVBVB"].ToString(); 
          or1.Parameters.Add("KDVBVG", OracleType.NVarChar).Value = dwData["KDVBVG"].ToString(); 
          or1.Parameters.Add("KDVBVERBNR", OracleType.NVarChar).Value = dwData["KDVBVERBNR"].ToString(); 
          or1.Parameters.Add("KDVBMITGLNR", OracleType.NVarChar).Value = dwData["KDVBMITGLNR"].ToString(); 
          or1.Parameters.Add("KDVBKONTONR", OracleType.NVarChar).Value = dwData["KDVBKONTONR"].ToString(); 
          or1.Parameters.Add("KDVBMITGLNAM", OracleType.NVarChar).Value = dwData["KDVBMITGLNAM"].ToString(); 
          or1.Parameters.Add("KDVBZENTRR", OracleType.NVarChar).Value = dwData["KDVBZENTRR"].ToString(); 
          or1.Parameters.Add("KDVBSTAKZ", OracleType.NVarChar).Value = dwData["KDVBSTAKZ"].ToString(); 
          or1.Parameters.Add("KDVBSTADAT", OracleType.NVarChar).Value = dwData["KDVBSTADAT"].ToString(); 
          or1.Parameters.Add("KDVBUVERBKZ", OracleType.NVarChar).Value = dwData["KDVBUVERBKZ"].ToString(); 
          or1.Parameters.Add("KDVBKOND", OracleType.NVarChar).Value = dwData["KDVBKOND"].ToString(); 
          or1.Parameters.Add("KDVBDATVON", OracleType.NVarChar).Value = dwData["KDVBDATVON"].ToString(); 
          or1.Parameters.Add("KDVBDATBIS", OracleType.NVarChar).Value = dwData["KDVBDATBIS"].ToString(); 
          or1.Parameters.Add("KDVBMAILGKZ", OracleType.NVarChar).Value = dwData["KDVBMAILGKZ"].ToString(); 
          or1.Parameters.Add("KDVBFILLER", OracleType.NVarChar).Value = dwData["KDVBFILLER"].ToString(); 
          or1.Parameters.Add("KDVBAENDZL", OracleType.NVarChar).Value = dwData["KDVBAENDZL"].ToString(); 
          or1.Parameters.Add("KDVBAENDBEN", OracleType.NVarChar).Value = dwData["KDVBAENDBEN"].ToString(); 
          or1.Parameters.Add("KDVBAENDDAT", OracleType.NVarChar).Value = dwData["KDVBAENDDAT"].ToString(); 
          or1.Parameters.Add("KDVBNEUBEN", OracleType.NVarChar).Value = dwData["KDVBNEUBEN"].ToString(); 
          or1.Parameters.Add("KDVBNEUDAT", OracleType.NVarChar).Value = dwData["KDVBNEUDAT"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
